package com.gxuwz.library_consumer.controller;

import com.github.pagehelper.PageInfo;
import com.gxuwz.commonmodule.entity.User;
import com.gxuwz.library_consumer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    /**
     * 不同功能对应的视图文件名称,注意不能包含后缀名
     */
    private String view_main = "userMain";//用户主页
    private String view_list = "user/user_list";//用户信息列表
    private String view_input = "user/user_add";//添加用户信息
    private String view_edit = "user/user_edit";//编辑用户信息
    private String view_info = "user/user_message";//用户个人信息
    private String redirect_list = "redirect:/user/findPage?pageIndex=1";

    @Autowired
    private UserService userService;

    /**
     * 读者首页
     * @return
     */
    @RequestMapping("/userMain")
    public String toUserMain(){
        return view_main;
    }

    /**
     * 读者个人信息
     * @param username
     * @param model
     * @return
     */
    @GetMapping("/{username}")
    public String getUser(@PathVariable("username") String username, Model model) {
        User user = userService.findInfo(username);
        model.addAttribute("user", user);
        return view_info;
    }

    /**
     * 读者信息列表
     * @return
     */
    @GetMapping("/list")
    public ModelAndView listUser(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(view_list);
        List<User> userList = userService.list(null);
        modelAndView.addObject("userList",userList);
        return modelAndView;
    }

    /**
     * 跳入增加读者视图
     * @return
     */
    @RequestMapping("/input")
    public String input(){
        return view_input;
    }

    /**
     * 增加读者信息
     * @param user
     * @return
     */
    @PostMapping("/add")
    public String add(User user){
        userService.insert(user);
        return redirect_list;
    }

    /**
     * 通过username删除读者信息
     * @param username
     * @return
     */
    @GetMapping("/delete")
    public String delete(String username){
        userService.deleteUser(username);
        System.out.println("删除成功");
        return redirect_list;
    }

    /**
     * 跳转到编辑读者视图
     * @param username
     * @param model
     * @return
     */
    @GetMapping("/get")
    public String get(String username, Model model){
        User user = userService.findById(username);
        model.addAttribute("m",user);
        return view_edit;
    }

    /**
     * 编辑读者信息
     * @param user
     * @return
     */
    @PostMapping("/update")
    public String update(User user){
        userService.updateUser(user);
        return redirect_list;
    }

    /**
     * 分页查询（不带条件）
     * @param model
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public String findAll(Model model, @RequestParam(value = "pageIndex",defaultValue = "1")int pageIndex,
                          @RequestParam(value = "pageSize",defaultValue = "4",required = false)int pageSize){
        PageInfo page = userService.findPage(pageIndex,pageSize);
        model.addAttribute("path","/user/findPage?pageIndex=");
        model.addAttribute("page",page);
        return view_list;
    }

}
